package com.mcptt.config.body.info.mcdata.binary;

import com.cloudptt.api.pjsip.api.MessageInfo;
import com.cloudptt.api.product.log.Log;

import java.nio.charset.StandardCharsets;

public class NtfSignalBody extends BinaryBody{
    protected static final byte TYPE_LEN = 1;
    protected static final byte STATUS_LEN = 1;
    protected static final byte DATE_LEN = 5;
    protected static final byte CONVERSATIONID_LEN = 16;
    protected static final byte MESSAGEID_LEN = 16;
    protected static final byte BODY_LEN = TYPE_LEN + STATUS_LEN + DATE_LEN + CONVERSATIONID_LEN + MESSAGEID_LEN;
    public NtfSignalBody()
    {

    }
    public static String toBodyStr(boolean isFd,MessageInfo info)
    {
        Log.debug(NtfSignalBody.class,"toBodyStr isFd =  " + isFd + " info =  " + info);

        byte[] allArray = new byte[BODY_LEN];
        allArray[0] = SDS_NOTIFYCATION;
        if(isFd)
        {
            allArray[0] = FD_NOTIFICATION;
        }
        int index = 1;

        allArray[index] = info.getStatus();
        index = index +1;

        byte[] dateArr = longToByteArray(info.getDate());
        System.arraycopy(dateArr, 0, allArray, index, dateArr.length);
        Log.debug(NtfSignalBody.class,"toBodyStr index = " + index +" data dateArr = ",dateArr);
        Log.debug(NtfSignalBody.class,"toBodyStr index = " + index +" allArray = ",allArray);

        index = index + dateArr.length;

        dateArr = info.getConversationIdByteArr();
        Log.debug(NtfSignalBody.class,"toBodyStr index = " + index +" ConversationId dateArr = ",dateArr);
        System.arraycopy(dateArr, 0, allArray, index, dateArr.length);
        Log.debug(NtfSignalBody.class,"toBodyStr index = " + index,allArray);

        index = index + dateArr.length;

        dateArr = info.getMsgId().getBytes();
        Log.debug(NtfSignalBody.class,"toBodyStr index = " + index+" MsgId dateArr = ",dateArr);
        System.arraycopy(dateArr, 0, allArray, index, dateArr.length);
        Log.debug(NtfSignalBody.class,"toBodyStr index = " + index,allArray);

        return byteArrayToHexStr(allArray);
    }


    protected static MessageInfo parseSignalPayload(byte signalType,byte[] contentArr)
    {
        MessageInfo info = new MessageInfo(signalType);
        Log.debug(NtfSignalBody.class,"parseSignallingPayload BODY_LEN =  " + BODY_LEN + " contentArr.length = " + contentArr.length);
        // 检查索引和子组长度是否有效
//        if (BODY_LEN != contentArr.length
//                && BODY_LEN + 2 != contentArr.length) {
//            Log.debug(NtfSignalBody.class,"parseSignallingPayload Invalid start index or subgroup length, cannot extract subContentArr.");
//            return info;
//        }
        int startIndex = 1;
        byte status = contentArr[startIndex];
        info.setStatus(status);

        startIndex = startIndex +1;

        int len = 5;
        byte[] dataTimeArr = new byte[len];
        System.arraycopy(contentArr, startIndex, dataTimeArr, 0, len);
        Log.debug(NtfSignalBody.class,"parseSignallingPayload dataTimeArr =  " ,dataTimeArr);
        long dataSec = byteArrayToLong(dataTimeArr);
        Log.debug(NtfSignalBody.class,"parseSignallingPayload dataSec =  " + dataSec);
        info.setDate(dataSec);

        startIndex = startIndex + 5;
        len = 16;
        byte[] conversationIdArr = new byte[len];
        System.arraycopy(contentArr, startIndex, conversationIdArr, 0, len);
        Log.debug(NtfSignalBody.class,"parseSignallingPayload conversationIdArr =  " ,conversationIdArr);
        String conversationId = new String(conversationIdArr);
        Log.debug(NtfSignalBody.class,"parseSignallingPayload conversationId =  " + conversationId);
        info.setConversationId(conversationId);

        startIndex = startIndex + 16;
        len = 16;
        byte[] msgIdArr = new byte[len];
        System.arraycopy(contentArr, startIndex, msgIdArr, 0, len);
        Log.debug(NtfSignalBody.class,"parseSignallingPayload msgIdArr =  " ,msgIdArr);
        String msgId = new String(msgIdArr);
        Log.debug(NtfSignalBody.class,"parseSignallingPayload msgId =  " + msgId);
        info.setMsgId(msgId);

        return info;
    }
}
